package com.dora.rabbitmq.config;

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.core.TopicExchange;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * topic交换机模型，需要一个topic交换机，两个队列和三个binding
 *
 * @author Clarence
 * @date 2024/03/03
 */
@Configuration
public class TopicExchangeConfig {
    @Bean
    public TopicExchange topicExchange() {
        return new TopicExchange("mytopic");
    }

    @Bean
    public Queue topicQueue1() {
        return new Queue("topicqueue1");
    }

    @Bean
    public Queue topicQueue2() {
        return new Queue("topicqueue2");
    }

    /**
     * 3个binding将交换机和相应队列连起来
     *
     * @return {@link Binding}
     */
    @Bean
    public Binding bindingTopic1() {
        //binding key
        return BindingBuilder.bind(topicQueue1()).to(topicExchange()).with("*.orange.*");
    }

    @Bean
    public Binding bindingTopic2() {
        return BindingBuilder.bind(topicQueue2()).to(topicExchange()).with("*.*.rabbit");
    }

    @Bean
    public Binding bindingTopic3() {
        //#表示0个或若干个关键字，*表示一个关键字
        return BindingBuilder.bind(topicQueue2()).to(topicExchange()).with("lazy.#");
    }
}
